package il.technion.cs236369.common;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * Encompasses some basic functionalities
 * 
 * @author Gal Lalouche
 * 
 */
public class Common {
	private static PrintStream	out;

	static {
		final boolean writeToConsole = false;

		if (writeToConsole) {
			out = System.out;
		} else {
			try {
				out = new PrintStream(new FileOutputStream("NUL:"));
			} catch (FileNotFoundException e) {
				assert false;
			}
		}

	}

	/**
	 * Gets the common PrintStream
	 * @return The common PrintStream
	 */
	public static PrintStream getOut() {
		return out;
	}
	
	/**
	 * Converts an input stream to a String
	 * @param is The input stream to convert
	 * @return The contents of the input stream as a String
	 */
	public static String convertStreamToString(InputStream is) {
		try {
			return new Scanner(is).useDelimiter("\\A").next();
		} catch (java.util.NoSuchElementException e) {
			return "";
		}
	}
}
